package com.example.adapter;

import java.util.ArrayList;
import java.util.List;

import com.example.consts.City;
import com.example.dianping.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import android.content.Context;
import android.opengl.Visibility;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class CityListAdapter extends BaseAdapter {

	private List<City> datas;
	private Context context;

	private StringBuffer buffer = new StringBuffer();// ������һ�α�������ĸ������
	private List<String> firstList = new ArrayList<String>();// ���������������

	public CityListAdapter(Context context, List<City> datas) {
		this.datas = datas;
		this.context = context;
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = LayoutInflater.from(parent.getContext()).inflate(
					R.layout.city_layout_item, null);
			ViewUtils.inject(viewHolder, convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		City city = datas.get(position);
		String sort = city.getSortKey();
		String name = city.getName();

//		viewHolder.city_name.setText(name);
//		viewHolder.sortKey.setText(sort);
		
		if(buffer.indexOf(sort) == -1){
			buffer.append(sort);
			firstList.add(name);
		}
			System.out.println(position +"----" +city.getName());
		if(firstList.contains(name)){
			viewHolder.sortKey.setText(sort);
			viewHolder.sortKey.setVisibility(View.VISIBLE);
		}else{
			viewHolder.sortKey.setVisibility(View.GONE);
		}
		viewHolder.city_name.setText(name);
		return convertView;
	}

	public class ViewHolder {
		@ViewInject(R.id.tv_city_list_item_sort)
		public TextView sortKey;
		@ViewInject(R.id.tv_city_list_item_name)
		public TextView city_name;

	}

}
